package cc.vv.lkimagecomponent2.glideLib;

import android.content.Context;
import android.support.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
import com.bumptech.glide.load.model.DataUrlLoader;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import com.bumptech.glide.load.model.StringLoader;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

import java.io.InputStream;

import cc.vv.lkimagecomponent2.initplugin.InitConfig;
import cc.vv.lkimagecomponent2.initplugin.InitPlugin;
import cc.vv.lklibrary.log.LogOperate;

/**
 * glide懒加载 初始化类
 *
 * @author dupeng dupeng@vv.cc
 * @date 2018/5/2
 */

@GlideModule
public class MainGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
        InitConfig config = InitPlugin.getInstance().mConfig;
        if (config != null) {
            //1.设置sd卡缓存数据
            builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context, config
                    .getCacheFileName(), config.getCacheMaxSize() * 1024 * 1024));
//            builder.setDiskCache()
            RequestOptions requestOptions = new RequestOptions();
            //2.设置缓存模式
            switch (config.getDiskCacheStrategy()) {
                case ALL:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
                    break;
                case NONE:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
                    break;
                case RESOURCE:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
                    break;
                case AUTOMATIC:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
                    break;
                default:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
                    break;
            }
            //3.设置默认占位图和error图
            if (config.getPlaceHolderRes() != 0) {
                requestOptions.placeholder(config.getPlaceHolderRes());
            }
            if (config.getErrorRes() != 0) {
                requestOptions.error(config.getErrorRes());
            }
            if (config.getPlaceHolderDrawable() != null) {
                requestOptions.placeholder(config.getPlaceHolderDrawable());
            }
            if (config.getErrorDrawable() != null) {
                requestOptions.error(config.getErrorDrawable());
            }

            //4.设置图片显示质量
            requestOptions.encodeQuality(config.getQuality());
            builder.setDefaultRequestOptions(requestOptions);

        } else {
            LogOperate.w("Glide applyOption初始化时,config为空,glide设置全为默认参数!");
        }
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
//        registry.replace(String.class, InputStream.class, new ModelLoaderFactory<String, InputStream>() {
//            @NonNull
//            @Override
//            public ModelLoader<String, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
//
//                return null;
//            }
//
//            @Override
//            public void teardown() {
//
//            }
//        });
    }
}

